package airportCoordinationMonitor;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import Config.ConfigReader;

public class Launcher {
	
	private static String COORDINATOR_EXE;

	public static void main(String[] args) {
		int active_port = 0, pasive_port = 0, monitor_port = 0;
		
		List<PacketLog> pings = new LinkedList<PacketLog>();
		Lock lock = new ReentrantLock();
			
		try{
			active_port = ConfigReader.getInt("active_port");
			pasive_port = ConfigReader.getInt("pasive_port");
			monitor_port = ConfigReader.getInt("monitor_port");
			COORDINATOR_EXE = ConfigReader.getString("coordinator_path");
		}
		catch (Exception ex) {
            System.out.print("Error leyendo configuración\n");
        }
		
		Process active_coord = null, pasive_coord = null;
		try{
			String[] active_exe = { COORDINATOR_EXE, "-a" };
			active_coord = new ProcessBuilder(active_exe).start();
			pasive_coord = new ProcessBuilder(COORDINATOR_EXE).start();
		}catch(Exception e){ System.out.print("FATAL ERROR executing coordinators: " + e.getMessage() + "\n"); System.exit(666); }

		Thread ph = new Thread(new PingHandler(active_port, pasive_port, pings, lock, active_coord, pasive_coord));
		Thread ih = new Thread(new IncomingHandler(monitor_port, pings, lock));
		
		ph.start();
		ih.start();
		
		System.out.append("System initiated\n");
		}
}
